package memory

import (
	. "com.mgface.disobj/common"
	log "github.com/sirupsen/logrus"
	"reflect"
)

func (cache *Memorystore) Get(key string, value []byte) (interface{}, error) {
	cache.Mutex.RLock()
	defer cache.Mutex.RUnlock()
	data := make([]interface{}, 0)
	if key == "dataNodes" || key == "apiNodes" || key == "metaNodes" {
		mvalues, exit := cache.Datas[key]
		if exit {
			log.Debug("操作:", key, ",value:", string(value), ",当前类型：", reflect.TypeOf(mvalues))
			//如果当前节点是slave，因为类型会丢失，所以需要单独处理
			if forceTxData, ok := mvalues.([]interface{}); ok {
				data = append(data, forceTxData...)
			} else if forceTxData, ok := mvalues.([]MetaValue); ok {
				for _, val := range forceTxData {
					data = append(data, val)
				}
			}
			return data, nil
		}
	} else if key == "metadata" {
		sysvalues, exit := cache.Datas[key]
		if exit {
			log.Debug("操作:", key, ",value:", string(value), ",当前类型：", reflect.TypeOf(sysvalues))
			forceTxData := sysvalues.(map[string][]Datadigest)

			if v, ok := forceTxData[string(value)]; ok {
				return v, nil
			}
		}
	} else if key == "filecrc" {
		sysvalues, exit := cache.Datas[key]
		if exit {
			log.Debug("操作:", key, ",value:", string(value), ",当前类型：", reflect.TypeOf(sysvalues))
			forceTxData := sysvalues.(map[string][]SharedData)
			if v, ok := forceTxData[string(value)]; ok {
				return v, nil
			}
		}
	} else {
		return cache.Datas[key], nil
	}
	return data, nil
}
